﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.Ajax
{
    public class CommentReplyListAjax : YouHP.Pages.BasePage
    {

        protected UserInfo ProfileUserData;
        protected List<CommentInfo> CommentReplyDataList;
        protected int PageIndex;
        protected int DataCount;
        protected int PageCount;
        protected string PagingString;


        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;


            int uid = RequestTools.GetQueryInt("uid", 0);
            ProfileUserData = UserBLL.Instance.GetUserInfo(uid);
            if (ProfileUserData.Id <= 0) return;

            //生成XML
            PageIndex = RequestTools.GetQueryInt("page", 1);

            CommentReplyDataList = CommentBLL.Instance.GetCommentReplyList(10, PageIndex, ProfileUserData.Id, out DataCount);

            PageCount = Utils.GetPageCount(DataCount, 10);

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pl_title\">" + "<span style=\"float:left;\">" + ProfileUserData.UserName + "收到的留言回复</span>" + "<span style=\"float:right;\">" + "共<b><font name=\"fedcount\" id=\"fedcount\">" + DataCount + "</font></b>条</span></div>");
            sb.Append("<ul class=\"hugbook\" id=\"hugbook\">");

            foreach (CommentInfo item in CommentReplyDataList)
            {
                //此次对话的条目数
                int Count;
                int replyCount = CommentBLL.Instance.GetReplyListByCommentId(1000000000, 1, item.ParentId, out Count).Count + 1;
                
                sb.Append("<li><div style=\"padding-left:20px; text-align:center; float:left;\">");
                string TempUrl = "/Profile/Index.aspx?uid=" + item.PostUserId;
                sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\"><img class=\"avatar\" width=\"45\" height=\"45\" src=\"" + item.PostAvatar + "\"/>");
                sb.Append("</a></div>");
                sb.Append("<div style=\"margin:0pt 10px 0pt 82px;\"><p class=\"graytext\"><span style=\"float:right;\">");
                if (UserData.Id > 0)
                {
                    if (UserData.Id != item.PostUserId && UserData.Id == item.HostUserId)
                    {
                        TempUrl = "/User/CommentConversation.aspx?uid=" + item.HostUserId + "&parentId=" + item.ParentId + "&reply=1";
                        sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\"><font color=#949494>回复</font></a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }

                    if (item.PostUserId == UserData.Id || item.HostUserId == UserData.Id)
                    {

                        sb.Append("<a href=\"javascript:if(window.confirm(\'确定删除该回复？\')) CommentDelete(" + item.Id + ");\"><font color=#949494>删除</font></a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }
                }
                TempUrl = "/Profile/Index.aspx?uid=" + item.PostUserId;
                sb.Append("</span><a class=\"client_name\" href=\"" + this.GetUrl(TempUrl) + "\">" + item.PostUserName + "</a>");
                TempUrl = item.PostTime.ToString("yyyy-MM-dd  HH:mm");
                sb.Append("&nbsp;" + TempUrl + "</p><p class=\"longtextfix\">" + item.Body + "</p>");
                TempUrl = "/User/CommentConversation.aspx?uid=" + item.HostUserId+"&parentId="+item.ParentId;
                if (replyCount > 0)
                {
                    sb.Append("<p style=\"float:right;\"><span style=\"color:#949494\">共有<font color=red>" + replyCount + "</font>条回话</span>" + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\"><font color=Green>+展开</font></a></p>");
                }
                sb.Append("</div>");

            }
            sb.Append("</ul>");

            PagingString = this.GetPagingString2(PageIndex, PageCount, "", "javascript:ViewCommentPage");
            sb.Append("<div class=\"pages\" style=\"padding:10px 5px;\">" + PagingString + "</div>");

            ResponseXML(sb);




        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}

